package com.seis_645.appTesting;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class DealerInfoActivity extends Activity {

	List<SpinnerInfo> spinners;
	private int cleanliness, supply, friendliness;
	private EditText name, street, city, state, zipcode;
	DealerDataSource datasource;
	AddressDataSource addrDatasource;
	Dealer dealer;
	Address address;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(
				R.layout.add_dealer, null);
		this.setContentView(viewToLoad);
		datasource = new DealerDataSource(this);
		datasource.open();
		Intent intent = getIntent();
		long id = intent.getLongExtra("dealer_id", -1);
		dealer = datasource.get_dealer(id);
		datasource.close();
		name = (EditText) findViewById(R.id.name);
		street = (EditText) findViewById(R.id.street);
		city = (EditText) findViewById(R.id.city);
		state = (EditText) findViewById(R.id.state);
		zipcode = (EditText) findViewById(R.id.zipcode);
		addrDatasource = new AddressDataSource(this);
		addrDatasource.open();
		address = addrDatasource.get_address(dealer.get_addr_id());
		addrDatasource.close();
		datasource.open();
		name.setText(dealer.get_name());
		street.setText(address.get_street());
		city.setText(address.get_city());
		state.setText(address.get_state());
		zipcode.setText(address.get_zipcode());
	
		spinners = new ArrayList<SpinnerInfo>();
		spinners.add(new SpinnerInfo(
				(Spinner) findViewById(R.id.cleanlinessSpinner), dealer
						.get_cleanliness()));
		spinners.add(new SpinnerInfo(
				(Spinner) findViewById(R.id.friendlinessSpinner), dealer
						.get_friendliness()));
		spinners.add(new SpinnerInfo(
				(Spinner) findViewById(R.id.supplySpinner), dealer.get_supply()));
		for (int i = 0; i < spinners.size(); i++) {
			fillSpinner(spinners.get(i).get_spinner(), R.array.ratings_array,
					spinners.get(i).get_selection());
		}
		
		Button cancel = (Button) findViewById(R.id.btn_cancel_dealer);
		Button save = (Button) findViewById(R.id.btn_save_dealer);
		OnTouchListener cancelListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					datasource.close();
					finish();
					// return true;
				}
				return false;
			}
		};

		OnTouchListener saveListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					addToDatabase();
					datasource.close();
					finish();
				}
				return false;
			}
		};

		cancel.setOnTouchListener(cancelListener);
		save.setOnTouchListener(saveListener);


	}

	private void addToDatabase() {
		int value;
		for (int i = 0; i < spinners.size(); i++) {
			String compare = spinners.get(i).get_spinner().getSelectedItem()
					.toString();
			if (compare.equalsIgnoreCase("excellent")
					|| compare.equalsIgnoreCase("five")) {
				value = 5;
			} else if (compare.equalsIgnoreCase("very good")
					|| compare.equalsIgnoreCase("four")) {
				value = 4;
			} else if (compare.equalsIgnoreCase("good")
					|| compare.equalsIgnoreCase("three")) {
				value = 3;
			} else if (compare.equalsIgnoreCase("fair")
					|| compare.equalsIgnoreCase("two")) {
				value = 2;
			} else {
				value = 1;
			}
			switch (i) {
			case 0:
				cleanliness = value;
				break;
			case 1:
				friendliness = value;
				break;
			case 2:
				supply = value;
				break;
			}
		}
		
		datasource.open();

		datasource.update_dealer(dealer.get_id(), dealer.get_addr_id(), name.getText().toString(), street.getText()
				.toString(), city.getText().toString(), state.getText()
				.toString(), zipcode.getText().toString(), cleanliness, friendliness, supply);

		datasource.close();

	}

	private void fillSpinner(Spinner spinner, int res, int selection) {
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, res, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		System.out.println(selection);
		// subtract 1 to offset because the database goes 1 - 5 instead of 0 - 4
		// 5 is the 0th in the list
		spinner.setSelection(5-selection);
	}
	
	@Override
	public void onPause() {
		datasource.close();
		super.onPause();
	}

	@Override
	public void onDestroy() {
		datasource.close();
		super.onDestroy();
	}

}
